<?php
/**
 * (actions)
 */
namespace App\Admin\Actions\Reimburse;

use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
class BatchPaid extends BatchAction
{
    public $name = '批量付款';

    public function handle(Collection $collection)
    {
        try{
            if($collection->isEmpty()){
                return $this->response()->error('请选择单据！');
            }
            $paidAccountNo = [];
            $paidCountryId = [];
            $flag = true;
            foreach($collection as $model){
                if(!in_array($model->paid_method,[2,3,4,6])){
                    $flag = false;
                    break;
                }
                if($model->audit_status != 2){
                    return $this->response()->error('单据:'.$model->reimburse_no.'未审核！');
                }
                if($model->paid_status != 1){
                    $flag = false;
                    break;
                }
                if(in_array($model->paid_method,[2,3,4])){
                    $paidAccountNo[$model->paid_account_no] = $model->paid_account_no;
                }else{
                    $paidCountryId[$model->country_id] = $model->country_id;
                }
            }
            if(count($paidAccountNo) > 1)$flag = false;
            if(count($paidCountryId) > 1)$flag = false;
            if($flag){
                $ids = $collection->pluck('id')->implode(',');
                $href = '/xqc/reimburse/batch-paid?id='.$ids;
                return $this->response()->redirect($href);
            }else{
                return $this->response()->error('请选择支付方式为银行卡/支付宝/PayPal的同一支付账号或Amazon礼品卡的同一国家下已审核的未付款订单！');
            }
        }catch (\Exception $e){
            report($e);
            return $this->response()->error('系统异常！');
        }
    }
}
